package com.bifrost.repository;

import com.bifrost.constant.MerchantConfigurationType;
import com.bifrost.entity.MerchantConfiguration;
import com.bifrost.entity.MerchantConfigurationAlias;
import com.bifrost.entity.MerchantStore;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface MerchantConfigurationRepository extends JpaRepository<MerchantConfiguration, Long> ,JpaSpecificationExecutor<MerchantConfiguration>{

	@Query("select m from MerchantConfiguration m join fetch m.merchantStore ms where ms.id=?1")
	List<MerchantConfiguration> findByMerchantStore(Long id);

	@Query("select m from MerchantConfiguration m join fetch m.merchantStore ms where ms.id=?1 and m.key=?2")
	MerchantConfiguration findByMerchantStoreAndKey(Long id, String key);

	@Query("select m from MerchantConfiguration m join fetch m.merchantStore ms where ms.id=?1 and m.merchantConfigurationType=?2")
	List<MerchantConfiguration> findByMerchantStoreAndType(Long id, MerchantConfigurationType type);

	@Query(value = "select * from merchant_configuration m where m.MERCHANT_ID=?1 and  m.TYPE=?2 and  m.CONFIG_KEY=?3" , nativeQuery = true)
	List<MerchantConfiguration> findByMerchantStoreAndTypeAndKey(Long merchantId, String type , String key);

	@Query("select m from MerchantConfiguration m join fetch m.merchantStore ms where ms.id=?1 and m.merchantConfigurationType=?2 order by m.key+0 desc")
	List<MerchantConfiguration> findByMerchantStoreAndTypeDesc(Long id, MerchantConfigurationType type);

	List<MerchantConfiguration> findByMerchantStoreAndConfigurationAlias(MerchantStore store , MerchantConfigurationAlias alias);

	@Query(value = "select DISTINCT new MerchantConfiguration(m.merchantConfigurationType,m.configurationAlias) from MerchantConfiguration m inner join m.merchantStore ms where ms.id=?1")
	List<MerchantConfiguration> findTypeByMerchantStore(Long merchantId);
}
